<?php
use \Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Response as FacadesResponse;

class StoreController extends BaseController {
    protected $layout = 'layouts.master';
	
	public function listProducts()
	{
        try {
        	$products = Product::getProducts();
        } catch (Exception $ex) {
            Log::error($ex);
            return $this->makeView(
                'errors.503', 
                '咨询律师失败，请稍后再试。',
                503
            );
        }
        foreach($products as $product) {
        	$product->life = $product->getProductLife();
        	$product->picture = UtilsHelper::getImage($product->picture, NULL);
        }
        $products->load('attribute');
        return $this->makeView('store.index', $products->toArray());
	}
	
	public function view($id)
	{
         try {
             $product = Product::findOrFail($id);
         } catch(Exception $ex) {
         	Log::error($ex);
            return $this->makeView(
                'errors.404',
                '没有找到符合要求的纪录。',
                404
            );
        }
        $product->load('attribute');
        $product->life = $product->getProductLife();
        $product->sum_price = $product->price * $product->life;
        $product->picture = UtilsHelper::getImage($product->picture, NULL);
         return $this->makeView('store.item', $product->toArray());
	}
	
	public function productComments($id){
		$evaluates = Evaluate::leftJoin('posts', 'evaluate.item_id', '=', 'posts.id')
			->leftjoin('post_order', 'posts.id', '=', 'post_order.post_id')
			->leftjoin('order_item', 'post_order.order_id', '=', 'order_item.order_id')
			->select(
				'evaluate.score',	
				'evaluate.comment',	
				'evaluate.created_at',	
				'evaluate.creator_id',	
				'posts.title',
				'post_order.order_id',
				'order_item.qty')
			->where('posts.status', '=', Post::STATUS_COMPLETE)
			->where('posts.type', '=', Post::TYPE_PAY_COMPANY)
			->where('evaluate.type', '=', Evaluate::TYPE_QUESTION)
			->where('order_item.product_id', '=', $id)
			->get();
		foreach($evaluates as $evaluate){
			$evaluate['user'] = $evaluate->getUserInfo();
		}
		return $this->makeView('general.200', $evaluates->toArray(), 200);
	}

	// 法律顾问银卡
	public function storeAdviser1()
	{
        return $this->makeView('store.adviser1');
	}
	// 法律顾问金卡
	public function storeAdviser2()
	{
        return $this->makeView('store.adviser2');
	}
	// 法律顾问白金卡
	public function storeAdviser3()
	{
        return $this->makeView('store.adviser3');
	}
	
}